Skip to content

fix: skip empty repositories instead of crashing#591

Open
wjglerum wants to merge 2 commits into
github-community-projects:mainfrom
wjglerum:fix/empty-repo-404
Open

fix: skip empty repositories instead of crashing#591
wjglerum wants to merge 2 commits into
github-community-projects:mainfrom
wjglerum:fix/empty-repo-404

Conversation

@wjglerum

@wjglerum wjglerum commented Jul 6, 2026

Copy link
Copy Markdown

Problem

When Evergreen iterates over an organization that contains an empty repository (one created but never pushed to, so it has no commits), the run crashes:

github.GithubException.GithubException: This repository is empty.: 404
{"message": "This repository is empty.",
 "documentation_url": "https://docs.github.com/v3/repos/contents/#get-contents",
 "status": "404"}

check_optional_file() in exceptions.py calls repo.get_contents(...) to look for an existing dependabot.yml. For a repo where the file merely does not exist, PyGithub raises UnknownObjectException, which is caught and translated to OptionalFileNotFoundError. But for an empty repository, GitHub returns a 404 that PyGithub surfaces as the base GithubException ("This repository is empty."), not UnknownObjectException. That escapes the existing handler and aborts the whole run, so no other repositories in the org get processed.

Fix

Handle a 404 GithubException the same way as a missing optional file, so the repository is treated as having no config and skipped by the normal flow. Non-404 GithubExceptions (permissions, rate limits, server errors) are re-raised unchanged so genuine problems are not masked.

Tests

Added two cases to test_exceptions.py:

  • an empty-repository 404 (base GithubException) is translated to OptionalFileNotFoundError
  • a non-404 GithubException (e.g. 403) is re-raised unchanged

All existing tests still pass.

check_optional_file() only caught UnknownObjectException, but an empty
repository (one with no commits) returns a 404 that PyGithub raises as
the base GithubException ("This repository is empty."). That escaped the
handler and crashed the whole run when iterating an organization that
contains a freshly-created, never-pushed repo.

Handle a 404 GithubException the same way as a missing optional file so
the repository is skipped. Non-404 GithubExceptions are re-raised
unchanged. Adds tests for both cases.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions github-actions Bot added the fix label Jul 6, 2026
@wjglerum wjglerum marked this pull request as ready for review July 6, 2026 10:37
@jmeridth jmeridth requested a review from Copilot July 6, 2026 12:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves robustness when scanning GitHub organizations by preventing Evergreen from crashing on empty repositories (repositories with no commits) when checking for optional configuration files via PyGithub.

Changes:

  • Extend check_optional_file() to translate 404 GithubException (empty repo case) into OptionalFileNotFoundError.
  • Add unit tests covering the empty-repository 404 translation and ensuring non-404 GithubExceptions are still re-raised.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
exceptions.py Adds handling for base GithubException 404s (empty repos) to match existing optional-file behavior.
test_exceptions.py Adds test coverage for empty-repository 404 behavior and non-404 re-raise behavior.

Comment thread exceptions.py
Comment on lines +50 to +58
except GithubException as e:
# An empty repository (one with no commits) returns a 404 that PyGithub
# raises as the base GithubException ("This repository is empty.")
# rather than UnknownObjectException. Treat it the same as a missing
# optional file so the repository is skipped instead of crashing the run.
if e.status == 404:
raise OptionalFileNotFoundError(
status=e.status, data=e.data, headers=e.headers
) from e

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thank you. You're right that hardening check_optional_file alone just moves the crash: an empty repo returns None there, the loop doesn't skip it, and it then hits repo.get_contents("/") / .github/workflows / .devcontainer in dependabot_file.py, which only catch UnknownObjectException.

I've pushed a follow-up that skips empty repos up front instead:

  • Added an is_empty_repo(repo) helper (repo.size == 0, no extra API call since size is already on the listed repo object).
  • The main loop now skips empty repos right after the archived check, before any get_contents lookup, so none of the dependabot_file.py paths are reached for them.
  • Kept the check_optional_file GithubException 404 handling as a defensive backstop.
  • Added unit tests for is_empty_repo; full suite passes (184 passed).

This felt cleaner than broadening the exception handling at each get_contents call site, but happy to switch to a shared safe_get_contents-style helper if you'd prefer that centralization.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the shared helper you floated at the end here is worth taking, and I dug up a reason to prefer it over the size == 0 skip. size is GitHub's cached disk-usage number (recalculated asynchronously, roughly hourly), so it isn't a reliable stand-in for "no commits." I confirmed against live repos: jsplumb/jsplumb is public, not archived, has a commit and a README.md, and still reports size: 0. The current skip would log it as "empty repository" and pass it over, so Dependabot never gets enabled there, and freshly pushed repos hit the same window until the cache refreshes.

A small helper that swallows a 404 from either UnknownObjectException or the base GithubException covers the truly-empty case at every call site without the false positive, so we could drop the size == 0 gate entirely:

def list_contents_or_empty(repo, path):
    """Return directory contents at path, or [] when the path is missing or the
    repo is empty. A missing path 404s as UnknownObjectException; an empty repo
    404s as the base GithubException. Both mean "nothing here"."""
    try:
        return repo.get_contents(path)
    except GithubException as e:
        if e.status == 404:
            return []
        raise

Then the three dependabot_file.py loops become for file in list_contents_or_empty(repo, "/"): (and the same for .github/workflows and .devcontainer), their try/except UnknownObjectException blocks go away, and GithubException gets added to the import on line 10. Happy to keep an early skip as well if we base it on a more reliable signal.

Addresses review feedback: the previous change only hardened
check_optional_file(), but empty repositories also 404 in
dependabot_file.py (repo.get_contents on "/", ".github/workflows",
".devcontainer"), which only catch UnknownObjectException and would
still crash the run.

Add an is_empty_repo() helper (repo.size == 0) and skip empty repos at
the top of the main loop, before any content lookup, so none of the
downstream get_contents paths are reached for them. The check_optional_file
GithubException handling is kept as a defensive backstop.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@zkoppert

zkoppert commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Appreciate you taking the time to work on a fix for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants